package com.chaoyue.media.service.impl;


import com.chaoyue.base.constant.MediaProcessStatus;
import com.chaoyue.base.utils.ObjectUtils;
import com.chaoyue.media.beans.po.MediaFiles;
import com.chaoyue.media.beans.po.MediaProcess;
import com.chaoyue.media.beans.po.MediaProcessHistory;
import com.chaoyue.media.mapper.MediaFilesMapper;
import com.chaoyue.media.mapper.MediaProcessHistoryMapper;
import com.chaoyue.media.mapper.MediaProcessMapper;
import com.chaoyue.media.service.MediaFileProcessService;
import groovy.util.logging.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@SuppressWarnings({"all"})
/**
 * @author whr
 * @version 1.0
 * @description: 视频处理任务领取实现类
 * @date 2024/12/19 21:28
 */
@Service
@Slf4j
public class MediaFileProcessServiceImpl implements MediaFileProcessService {
    @Autowired
    private MediaProcessMapper mediaProcessMapper;
    @Autowired
    private MediaFilesMapper mediaFilesMapper;
    @Autowired
    private MediaProcessHistoryMapper mediaProcessHistoryMapper;

    @Override
    public List<MediaProcess> getMediaProcessList(int shardTotal, int shardIndex, int count) {
        return mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
    }

    /**
     * @description:  更新任务状态
      * @param: taskId
      * @param: status
      * @param: fileId
      * @param: url
      * @param: errorMsg 错误信息
     * @return:
     * @author whr
     * @date: 2023/5/20 17:24
     */
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        //该任务不存在,直接返回
        if (ObjectUtils.isEmpty(mediaProcess)) {
            return;
        }
        //任务处理失败
        if (status.equals(MediaProcessStatus.PROCESS_FAIL.getCode())) {
            mediaProcess.setStatus(MediaProcessStatus.PROCESS_FAIL.getCode());
            mediaProcess.setErrormsg(errorMsg);
            mediaProcess.setFailCount(mediaProcess.getFailCount() + 1);
            mediaProcessMapper.updateById(mediaProcess);
            return;
        }
        //任务处理成功
        //更新文件信息表
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if (mediaFiles != null) {
            mediaFiles.setUrl(url);
            mediaFilesMapper.updateById(mediaFiles);
        }
        //更新任务处理表
        mediaProcess.setUrl(url);
        mediaProcess.setStatus(MediaProcessStatus.PROCESS_SUCCESS.getCode());
        mediaProcess.setFinishDate(LocalDateTime.now());
        mediaProcessMapper.updateById(mediaProcess);
        //将任务插入历史任务表
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
        mediaProcessHistoryMapper.insert(mediaProcessHistory);
        //将任务删除任务处理表
        mediaProcessMapper.deleteById(mediaProcess.getId());
    }

    /**
     * @description: 乐观锁
     * @param: id 任务id
     * @return:
     * @author whr
     * @date: 2023/5/20 16:52
     */
    @Override
    public boolean startTask(Long id) {
        int i = mediaProcessMapper.startTask(id);
        return i <= 0 ? false : true;
    }
}
